<script lang="tsx">
/**
 * 编码规则
 */
export default {
  name: "code-rule"
}
</script>

<script lang="tsx" setup>
import {definePageModel, PageModel, processRequest} from "anqin-framework"
import {ref} from "vue"
import CodeRuleModel from "./CodeRuleModel.ts"
import APIS from "@/apis"
import {ElMessage, ElMessageBox} from "element-plus"
import {deleteMultiUrl} from "@/utils/base-url.ts"
import {getCode} from "@/utils/get-code.ts"
import {CodeRuleTypeEnum} from "@/utils/enum.ts"

/**
 * 页面模型
 */
const pageModelRef = ref()
const config = definePageModel<CodeRuleModel>({
  url: APIS.CODE_RULE_BASE,
  getDataBack(data) {
    console.log(data)
    return data
  },
  searchForm: {
    els: [
      {
        eType: 'el-input',
        prop: "code",
        props: {
          placeholder: "单据编码",
          clearable: true
        }
      },
      {
        eType: 'el-input',
        prop: "rule",
        props: {
          placeholder: "编码规则",
          clearable: true
        }
      }
    ]
  },
  programForm: {
    els: [
      {
        label: "新增",
        eType: 'el-button',
        props: {
          type: "primary"
        },
        event: "add"
      },
      {
        label: "删除",
        eType: 'el-button',
        props: {
          type: "danger"
        },
        event: "delete-multi"
      }
    ]
  },
  table: {
    page: {
      enable: true,
      pageSize: 20
    },
    selection: true,
    operation: {
      width: 100,
      els: [
        {
          eType: "el-button",
          label: "编辑",
          event: "edit",
          props: {
            type: "primary",
            link: true
          },
          hide: false
        },
        {
          eType: "el-button",
          label: "删除",
          event: "delete",
          props: {
            type: "primary",
            link: true
          },
          hide: false
        }
      ]
    },
    props: {
      stripe: false,
      border: false
    },
    els: [
      {
        label: "单据编码",
        prop: "code",
        minWidth: 120
      },
      {
        label: "编码规则",
        prop: "rule",
        minWidth: 120
      },
      {
        label: "当前编号",
        prop: "number",
        minWidth: 120
      },
      {
        label: "备注",
        prop: "remark"
      },
      {
        label: "创建时间",
        prop: "createTime",
        minWidth: 200
      }
    ]
  },
  form: {
    title: "新增/编辑",
    width: 500,
    sheetForm: {
      async bindData(data: any) {
        if (!data.id) {
          data.code = await getCode(CodeRuleTypeEnum.编码规则表)
        }
        return data
      },
      labelWidth: "100px",
      els: [
        {
          label: "单据编码",
          eType: "el-input",
          prop: "code",
          renderFn(data: any) {
            return <span>{data.code}</span>
          }
        },
        {
          label: "编码规则",
          eType: "el-input",
          prop: "rule",
          props: {
            placeholder: "请输入编码规则",
            clearable: true
          }
        },
        {
          label: "备注",
          eType: "el-input",
          prop: "remark",
          props: {
            placeholder: "请输入备注",
            clearable: true
          }
        }
      ]
    }
  }
})

/**
 * 刷新表格
 */
const handleRefresh = () => {
  pageModelRef.value.refreshTableData()
}

/**
 * 新增
 */
const handleAdd = () => {
  pageModelRef.value.handleAdd()
}

/**
 * 编辑
 */
const handleEdit = (row: any) => {
  pageModelRef.value.handleEdit(row)
}

/**
 * 删除
 */
const handleDelete = (row: any) => {
  pageModelRef.value.handleDelete(row)
}

/**
 * 删除 - 批量
 */
const handleDeleteMulti = async () => {
  const selectData = pageModelRef.value.getTableSelectData()
  if (!selectData) {
    ElMessage.warning("请先选择一条数据")
    return false
  }
  await ElMessageBox.confirm("是否确认删除?", "删除", {
    type: "error"
  })
  await processRequest.post(deleteMultiUrl(APIS.CODE_RULE_BASE),
      selectData.map((item: any) => item.id))
  ElMessage.success("删除成功")
  handleRefresh()
}
</script>

<template>
  <PageModel ref="pageModelRef" :PageModel="config" @add="handleAdd" @delete="handleDelete" @edit="handleEdit"
             @delete-multi="handleDeleteMulti"/>
</template>

<style lang="scss" scoped>
</style>
